/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.web.impl;

import cz.muni.fi.pa165.creatures.dao.utils.DAOUtils;
import cz.muni.fi.pa165.creatures.dao.web.FunctionDAO;
import cz.muni.fi.pa165.creatures.dao.web.PersonDAO;
import cz.muni.fi.pa165.creatures.dao.web.PersonFunctionDAO;
import cz.muni.fi.pa165.creatures.model.web.Function;
import cz.muni.fi.pa165.creatures.model.web.Person;
import cz.muni.fi.pa165.creatures.model.web.PersonFunction;
import cz.muni.fi.pa165.creatures.model.web.PersonFunctionPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.web.PersonFunction} instances.
 *
 * @author xmiklos1
 */
@Repository
@Transactional
public class PersonFunctionDAOImpl implements PersonFunctionDAO {

    @PersistenceContext
    EntityManager em;

    @Autowired
    PersonDAO personDAO;

    @Autowired
    FunctionDAO functionDAO;

    public PersonDAO getPersonDAO() {
        return personDAO;
    }

    public void setPersonDAO(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }

    public FunctionDAO getFunctionDAO() {
        return functionDAO;
    }

    public void setFunctionDAO(FunctionDAO functionDAO) {
        this.functionDAO = functionDAO;
    }

    @Override
    public PersonFunctionPK create(PersonFunction personFunction) {
        DAOUtils.notNull(personFunction);
        em.persist(personFunction);
        return personFunction.getPersonFunctionPK();
    }

    @Override
    public void update(PersonFunction personFunction) {
        DAOUtils.notNull(personFunction);
        em.merge(personFunction);
    }

    @Override
    public void remove(PersonFunction personFunction) {
        DAOUtils.notNull(personFunction);
        em.remove(personFunction);
    }

    @Override
    public PersonFunction get(PersonFunctionPK pk) {
        DAOUtils.notNull(pk);
        return em.find(PersonFunction.class, pk);
    }

    @Override
    public Long getCount() {
        return (Long) em.createQuery("select count(pf) from PersonFunction as pf")
                .getSingleResult();
    }

    @Override
    public List<PersonFunction> getAll() {
        return em.createQuery("from PersonFunction").getResultList();
    }

    @Override
    public void removeAll() {
        em.createQuery("delete from PersonFunction pf").executeUpdate();
    }

    @Override
    public boolean hasFunction(Person person, Function function) {
        DAOUtils.notNull(person);
        DAOUtils.notNull(function);
        DAOUtils.notNull(person.getName());
        DAOUtils.notNull(function.getName());
        List<Function> functions = getFunctions(person.getName());
        String functionName = function.getName();
        for (Function f : functions) {
            if (f.getName().equals(functionName)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Function> getFunctions(String name) {
        DAOUtils.notNull(name);
        Long personId = personDAO.getByName(name).getId();
        if (personId != null) {
            Query q = em.createQuery("select f from Function f where f.id in "
                    + "(select pf.personFunctionPK.functionId "
                    + "from PersonFunction pf "
                    + "where pf.personFunctionPK.personId = :person_id)");
            q.setParameter("person_id", personId);
            return q.getResultList();
        } else {
            return null;
        }
    }
}
